package org.jeecg.modules.FinanceModule.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.FinanceModule.entity.*;
import org.jeecg.modules.FinanceModule.mapper.FinanceMapper;
import org.jeecg.modules.FinanceModule.service.FinanceService;
import org.jeecg.modules.zonglzp.entity.User;
import org.jeecg.modules.zonglzp.entity.store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    FinanceMapper mapper;

    /*
    查询用户个人流水,订单
     */
    @Override
    public IPage<BusinessWaterEntity> getBusinessWaterlist(Page<BusinessWaterEntity> page, String userId) {
        return mapper.getBusinessWaterlist(page, userId);
    }

    /*
    查询用户个人充值明细
     */
    @Override
    public IPage<FinancialWaterEntity> getFinanceWaterlist(Page<FinancialWaterEntity> page, String userId, String date, String starttime, String endtime) {
        return mapper.getFinanceWaterlist(page, userId,date,starttime,endtime);
    }


    //查询之前已有的余额
    @Override
    public String getoldmoney(int userId) {
        return mapper.getoldmoney(userId);
    }

    //更改原有的余额
    @Override
    public void updateAccountBalance(int userId, String valueOf) {
        mapper.updateAccountBalance(userId, valueOf);
    }

    //往财务流水明细表里插入一条数据
    @Override
    public void insertAddmoneyWater(int userId, String money) {

        mapper.insertAddmoneyWater(userId, money, System.currentTimeMillis(), new Date());


    }

    //撤回充值的钱
    @Override
    public int rollbackMoney(int userId, int id, String money, int type) {

        if(type==2||type==3){
       String accountBalance=mapper.getoldmoney(userId);
       double MoneyInt = Double.parseDouble(accountBalance) - Double.parseDouble(money);
          mapper.updaterealAccountBalance(userId,String.valueOf(MoneyInt));
        }else{
            String oldmoney =getshengyuedu(userId);
            BigDecimal oldmoneyint=new BigDecimal(oldmoney);
            BigDecimal moneyint=new BigDecimal(money);
            //String oldmoney = getoldmoney(userId);
            //用现有的余额减去撤回的余额
            //  double MoneyInt = Double.parseDouble(oldmoney) - Double.parseDouble(money);
            //然后将减完的钱修改到这个人的账户下
            mapper.updatedikouedu(userId, String.valueOf(oldmoneyint.subtract(moneyint)));
        }



        //然后删除掉这条充值记录
        return mapper.deleteaddmoney(id);
    }


    //查询公司和用户表关联
    @Override
    public IPage<CompanyEntity> getcompanyList(Page<CompanyEntity> page, String companyName, String aboutPhone, String realName, int status) {
        return mapper.getcompanyList(page,companyName,aboutPhone,realName,status);
    }


    /*
    用公司编码查询详情
     */
    @Override
    public CompanyEntity getcompanydetail(String companyCode) {
        return mapper.getcompanydetail(companyCode);
    }

    //查询该公司下所有的业务流水
    @Override
    public IPage<BusinessWaterEntity> getCompanyBusinessList(Page<BusinessWaterEntity> page, String companyCode, int isAfter) {
        return mapper.getCompanyBusinessList(page, companyCode,isAfter);
    }


    //通过公司编码查询该公司所有员工
    @Override
    public IPage<User> getcompanystaff(Page<User> page, String companyCode) {
        return mapper.getcompanystaff(page, companyCode);
    }

    //查询该公司下所有门店
    @Override
    public IPage<store> getcompanystore(Page<store> page, String companyId) {
        return mapper.getcompanystore(page, companyId);
    }

    //查询该公司下所有品牌标
    @Override
    public IPage<Map<String, Object>> getcompanylabel(Page<Map<String, Object>> page, String companyId) {
        return mapper.getcompanylabel(page, companyId);
    }

    //往反冲表中插入一条记录
    @Override
    public void insertrecoiltable(String userId, int recoilType, String recoilMoney, String factoryResponsibility, String platformResponsibility, String remark) {
        mapper.insertrecoiltable(userId, recoilType, recoilMoney, factoryResponsibility, platformResponsibility, remark, new Date());
    }

    //在反冲操作后往财务流水表插入一条记录
    @Override
    public void insertcaiwuliushuibyfanchon(String userId, String recoilMoney, int recoilType, String remark) {
        mapper.insertcaiwuliushuibyfanchon(userId, recoilMoney, System.currentTimeMillis(), new Date(),recoilType,remark);
    }

    //查询该客户的开票记录
    @Override
    public IPage<InvoiceEntity> getKehukaipiaoList(Page<InvoiceEntity> page, String userId) {
        return mapper.getKehukaipiaoList(page,userId);
    }


    //开票记录页面的公司详情
    @Override
    public CompanyEntity getKehukaipiaoDetail(String userId) {
        return mapper.getKehukaipiaoDetail(userId);
    }

    // 根据userId获取开票金额
    @Override
    public String getInvoicedmoneybyuserId(String userId, int i) {
        return mapper.getInvoicedmoneybyuserId(userId,i);
    }

    //新增开票记录
    @Override
    public int addInvoice(InvoiceEntity entity) {
        entity.setCreateTime(new Date());
        return mapper.addInvoice(entity);
    }

    //新增对账记录
    @Override
    public int addReconciliationEntity(ReconciliationEntity entity) {
        entity.setCreateTime(new Date());
        return mapper.addReconciliationEntity(entity);
    }


    //通过用户id查询他的月结额度
    @Override
    public String getyuejieedubyid(int parseInt) {
        return mapper.getyuejieedubyid(parseInt);
    }

    //通过用户id查询他的剩余额度
    @Override
    public String getshengyuedu(int parseInt) {
        return mapper.getshengyuedu(parseInt);
    }

    //将用户的剩余额度改为月结额度
    @Override
    public void updateyuejietoshengxu(String yuejieedu, String shengyuedu, int parseInt) {
        mapper.updateyuejietoshengxu( yuejieedu,  shengyuedu,  parseInt);
    }

    @Override
    public void updaterealAccountBalance(int parseInt, String valueOf) {
        mapper.updaterealAccountBalance( parseInt,  valueOf);
    }

    //查询未开票金额
    @Override
    public String getNoEnterMoney(String userId) {
        return mapper.getNoEnterMoney(Integer.parseInt(userId) );
    }


    //修改用户的未开票金额
    @Override
    public void updateUserNoEnterMoney(String userId, double v) {
        mapper.updateUserNoEnterMoney(Integer.parseInt(userId) , v);
    }
}
